package com.wbdgj.ui.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.RelativeLayout;

import com.jph.takephoto.app.TakePhotoActivity;
import com.jph.takephoto.model.TResult;
import com.wbdgj.R;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.utils.CustomHelper;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;

import java.io.File;

import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;

public class PhotoUploadActivity extends TakePhotoActivity {
    private CustomHelper customHelper;
    private Context context = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        View contentView = LayoutInflater.from(this).inflate(R.layout.common_layout, null);
        setContentView(contentView);
        ButterKnife.bind(this);
        customHelper = CustomHelper.of(contentView);
        getWindow().getAttributes().width = RelativeLayout.LayoutParams.MATCH_PARENT;
        getWindow().getAttributes().gravity = Gravity.BOTTOM;
        setFinishOnTouchOutside(true);
    }

    public void onClick(View view) {
        customHelper.onClick(view, getTakePhoto());
    }

    @Override
    public void takeCancel() {
        super.takeCancel();
    }

    @Override
    public void takeFail(TResult result, String msg) {
        super.takeFail(result, msg);
    }

    @Override
    public void takeSuccess(final TResult result) {
        super.takeSuccess(result);
        RequestBody fileBody = RequestBody.create(MediaType.parse("image/png"), new File(result.getImage().getCompressPath().toString()));
        Call<ResultObjectModel> call = HttpAdapter.getSerives().addimg(fileBody, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel resultUploadModel) {

                if (resultUploadModel.getResultCode().equals("0000")){
                    ToastUtils.toastShort(resultUploadModel.getResultMsg());
                    setResult(1);
                    finish();
                }else{
                    ToastUtils.toastShort(resultUploadModel.getResultMsg());
                }

            }
        });
    }
    @OnClick(R.id.btn_back)
    public void onViewClicked() {
        finish();
    }
}
